package sdata.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import sdata.entity.Commodity;
import sdata.service.CommodityService;

import java.util.List;

@Api("商品管理")
@RestController
@RequestMapping("commodities")
public class CommodityController {
    @Autowired
    CommodityService commodityService;

    @ApiOperation("根据Id查询商品")
    @GetMapping("/{id}")
    public ResponseEntity<Commodity> getCommodity(@ApiParam("商品Id")@PathVariable long id){
        Commodity result = commodityService.getCom(id);
        if(result==null) {
            return ResponseEntity.noContent().build();
        }else{
            return ResponseEntity.ok(result);
        }
    }

    @ApiOperation("根据条件查询商品")
    @GetMapping("")
    public ResponseEntity<List<Commodity>> findCommodity(@ApiParam("商品名称")String name, @ApiParam("是否完成")Boolean ifsold){
        List<Commodity> result = commodityService.findCom(name);
        return ResponseEntity.ok(result);
    }

    @ApiOperation("添加商品")
    @PostMapping("")
    public ResponseEntity<Commodity> addCommodity(@RequestBody Commodity c){
        Commodity result = commodityService.addCom(c);
        return ResponseEntity.ok(result);
    }

    @ApiOperation("修改商品")
    @PutMapping("/{id}")
    public ResponseEntity<Void> updateCommodity(@PathVariable long id,@RequestBody Commodity c){
        commodityService.updateCom(id,c);
        return ResponseEntity.ok().build();
    }

    @ApiOperation("删除商品")
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteTodo(@PathVariable long id){
        commodityService.deleteCom(id);
        return ResponseEntity.ok().build();
    }



}
